package com.wyp168.leetcode;

/**
 * @ClassName IsIdealPermutationSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/11/16 22:03
 */
public class IsIdealPermutationSolution {
    public static void main(String[] args) {
        IsIdealPermutationSolution solution = new IsIdealPermutationSolution();
        int[] nums = {1, 0, 2};
        System.out.println(solution.isIdealPermutation(nums));
    }


    /**
     * 全局倒置：
     *        0 <= i < j < n
     *        nums[i] > nums[j]
     * 局部倒置
     *        0 <= i < n - 1
     *        nums[i + 1] < nums[i]
     * 要求： 判断给定的整型数组nums，全局倒置的数量和局部倒置的数量是否相等
     * @param nums 给定无重复元素的整型数组
     * @return 返回一个boolean类型的值，表示个数是否相等
     */
    public boolean isIdealPermutation(int[] nums) {
        //countA表示全局倒置的数量； countB表示局部倒置的数量
        int[] max = {0, nums[0]}, min = {0, nums[0]};
        for (int i = 1; i < nums.length; i ++) {
            int tempVal = nums[i];
            if (tempVal < max[1] && max[0] + 1 != i || tempVal < min[1]  && min[0] + 1 != i) {
                return false;
            }

            if (tempVal > max[1]) {
                max[0] = i;
                max[1] = tempVal;
            }

            if (tempVal < min[1]) {
                min[0] = i;
                min[1] = tempVal;
            }
        }
        return true;
    }
}
